﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmInsuranceList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int DocumentID;
        RoleRightInfo listRole;
        HeaderClient _wcfHeader;
        string _Type = "";
        public frmInsuranceList(string Type)
        {
            InitializeComponent();
            _wcfHeader = Service.Header();
            listRole = UIParamater.Quyen_PhatSinhBaoHiem;
            _Type = Type;
            if (_Type == "HS")
                this.Text = this.TabText = "Phát sinh BH hệ số";
            else
                this.Text = this.TabText = "Phát sinh BH mức lương";
            BuildVoucherGrid();
            LoadData();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                UISetting.MesNotRightNew();
                return;
            }
            //Form f;
            //if (_Type == "HS")
            //    f = new frmInsurance(0, listRole, 0, true);
            //else
            //    f = new frmInsurance(0, listRole, 0, false);
            //if (f.ShowDialog(this) == DialogResult.OK)
            //{
            //    currPage = 0;
            //    LoadData();
            //}
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "Document_ID", "", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Document_Date", "Ngày Ctừ", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Period", "Kỳ PS", "C", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Month", "Tháng", "C", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Year", "Năm", "C", 60, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Social", "BHXH", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Medical", "BHYT", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Document_Unemployed", "BHTN", "R", 80, true, true);
            UISetting.AddColumn(ref dgvList, "Document_BasicSalary", "ML tối thiểu", "R", 140, true, true);
        }
        private void LoadData()
        {
            DataSet _ds = _wcfHeader.Header_GetList(out itemCount, currPage, pageSize, Program.strBranchBHXH, DateTime.Now.Month, DateTime.Now.Year, _Type);
            int n = _ds.Tables[0].Rows.Count;
            PageIndex();
            dgvList.Rows.Clear();
            for (int i = 0; i < n; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["Document_ID"].Value = _ds.Tables[0].Rows[i]["Document_ID"].ToString();
                dgvList.Rows[i].Cells["Document_Date"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Document_Date"]).ToString("dd/MM/yyyy");
                dgvList.Rows[i].Cells["Document_Period"].Value = _ds.Tables[0].Rows[i]["Document_Period"].ToString();
                dgvList.Rows[i].Cells["Document_Month"].Value = _ds.Tables[0].Rows[i]["Document_Month"].ToString();
                dgvList.Rows[i].Cells["Document_Year"].Value = _ds.Tables[0].Rows[i]["Document_Year"].ToString();
                dgvList.Rows[i].Cells["Document_Social"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Document_Social"]).ToString("#0.0#");
                dgvList.Rows[i].Cells["Document_Medical"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Document_Medical"]).ToString("#0.0#");
                dgvList.Rows[i].Cells["Document_Unemployed"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Document_Unemployed"]).ToString("#0.0#");
                dgvList.Rows[i].Cells["Document_BasicSalary"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["Document_BasicSalary"]).ToString("#,##0");
            }
        }

        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    DocumentID = Convert.ToInt32(dgvList.CurrentRow.Cells["Document_ID"].Value);
                    //Form f = new frmInsurance(DocumentID, listRole, 0, true);
                    //if (f.ShowDialog(this) == DialogResult.OK)
                    //{
                    //    LoadData();
                    //}
                }
                else
                    UISetting.MesNotRightView();

            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage > 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void frmInsuranceList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.N:
                    if (e.Control)
                        if (btnNew.Enabled)
                            btnNew_Click(null, null);
                    break;

                case Keys.Delete:
                    if (e.Control)
                        if (btnDel.Enabled)
                            btnDel_Click(null, null);
                    break;
                case Keys.E:
                    if (e.Control)
                        if (btnEdit.Enabled)
                            btnEdit_Click(null, null);
                    break;
                case Keys.Escape:
                    btnCancel_Click(null, null);
                    break;
            }
        }

        private void frmInsuranceList_FormClosing(object sender, FormClosingEventArgs e)
        {
            _wcfHeader.Close();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                DocumentID = Convert.ToInt32(dgvList.CurrentRow.Cells["Document_ID"].Value);
                if (_wcfHeader.Header_Using(DocumentID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfHeader.Header_Delete(DocumentID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {

        }
    }
}
